package com.stu.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stu.common.R;
import com.stu.pojo.User;
import com.stu.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-12-20
 */
@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody User user){
        String username = user.getName();
        String password = user.getPass();
        log.info("username:{},password:{}",username,password);
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getName,username);
        User adminOne = userService.getOne(qw);
        //log.info("adminOne :username:{},password:{}",adminOne.getUsername(),adminOne.getPassword());
        if (adminOne == null){
            return R.error("用户不存在");
        }
        password = DigestUtils.md5DigestAsHex(user.getPass().getBytes(StandardCharsets.UTF_8));
        if (!adminOne.getPass().equals(password)){
            return R.error("密码错误");
        }
        request.getSession().setAttribute("user",adminOne.getId());
        return R.success("登陆成功");
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }

    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name){
        Page<User> userPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),User::getName,name);
        qw.orderByDesc(User::getUpdateTime);
        qw.orderByAsc(User::getName);
        userService.page(userPage,qw);
        return R.success(userPage);
    }

    @PostMapping
    public R save(@RequestBody User user){
        //user.setPass("1234");
        user.setStatus(1);
        user.setPass(
                DigestUtils.md5DigestAsHex(
                        user.getPass().getBytes(StandardCharsets.UTF_8))
        );
        userService.save(user);
        return R.success("新增成功");
    }

    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        User user = userService.getById(id);
        if (user != null){
            return R.success(user);
        }
        return R.error("没有查询到对应员工信息");
    }

    @PutMapping
    public R update(@RequestBody User user){
        boolean flag = userService.updateById(user);
        return flag ? R.success("修改成功"):R.error("修改失败");
    }

}

